package pogvue.gui;

import pogvue.datamodel.SequenceI;
import pogvue.gui.schemes.ColourSchemeI;

import java.awt.*;
import java.util.Hashtable;
import java.util.Vector;

public final class SequenceRenderer implements RendererI {
  protected final Color   color = Color.black;
  //  protected ColourSchemeI cs;

  public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i) {
    Color c = cs.findColour(seq,seq.getSequence(i,i+1),i,null);
    return c;
  }

  public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText,Vector freq, int seqnum,AlignViewport av, Hashtable props,int intpid[][]) {

    if (displayBoxes) {
      drawBoxes(g,cs,seq,start,end,x1,y1,(int)width, height,freq);
    }
    if (displayText) {
        drawText(g,cs,seq,start,end,x1,y1,(int)width,height);
    }
  }

  private void drawBoxes(Graphics g,ColourSchemeI cs, SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {
    int i      = start;
    int length = seq.getLength();

    Color currentColor = Color.white;

    int curStart = x1;
    int curWidth = width;
 
    int threshold = 80;

    while (i <= end && i < length) {
      Color c = getResidueBoxColour(cs,seq,i);

      Hashtable hash  = (Hashtable)freq.elementAt(i-start);
      String    s     = (String)hash.get("maxResidue");
      int       count = (Integer) hash.get("maxCount");
      int       max   = (Integer) hash.get("size");
      int       nongap = (Integer) hash.get("nongap");
      float     frac  = (float)(count*1.0/(1.0*nongap));

      //System.out.println("Frac/count/nongap " + frac + " " + count + " " + nongap);
      if (!seq.getSequence().substring(i,i+1).equals(s) ||
	  s.equals("-") ||
	  s.equals(".") ||
	  s.equals(" ")) {
	c = Color.white;
      } else {
	if (frac > 0.9) {
	  c = Color.red;
	} else if (frac > 0.8) {
	  c = Color.orange;
	} else if (frac > 0.7) {
	  c = Color.pink;
	} else if (frac > 0.5) {
	  c = Color.yellow;
	} else if (frac> 0.3) {
	    c = Color.lightGray;
	}
      }

      if (c != currentColor || c != null) {
        g.fillRect(x1+width*(curStart-start),y1,curWidth,height);

        currentColor = c;
        g.setColor(c);

        curStart = i;
        curWidth = width;
      } else {
        curWidth += width;
      }

      i++;
    }
    g.fillRect(x1+width*(curStart-start),y1,curWidth,height);
  }

  private void drawText(Graphics g, ColourSchemeI cs, SequenceI seq,int start, int end, int x1, int y1, int width, int height) {
    int pady = 2;
    g.setColor(Color.black);

    // Need to find the sequence position here.

    boolean fastDraw = true;

    if (fastDraw) {
	String s;
	if (end < seq.getLength()) {
	    s = seq.getSequence().substring(start,end+1);
        } else {
	    s = seq.getSequence().substring(start);
        }
	    g.drawString(s,x1,y1+height-pady);
	    
    } else {
      if (Debug.getDebugLevel() > 0) {
        System.out.println("Drawing sequence: ");
      }
      for (int i=start; i <= end; i++) {
        String s;

        if (i < end && i < seq.getLength()) {
          s = seq.getSequence().substring(i,i+1);
        } else {
          s = seq.getSequence().substring(i,i+1);
        }
        if (Debug.getDebugLevel() > 0) {
          System.out.print("|" + s + "|");
        }

        g.drawString(s,x1+width*(i-start),y1+height-pady);
      }
      if (Debug.getDebugLevel() > 0) {
        System.out.println("");
      }
    }
  }
}
